package experimental;

import java.io.File;
import java.io.IOException;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.lexer.Page;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import util.nodecloner.NodeCloner;
import wish.stages.ParsingStage;

public class HTMLTestParser {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws ParserException 
	 * @throws CloneNotSupportedException 
	 */
	public static void main(String[] args) throws IOException, ParserException, CloneNotSupportedException {
		
		File file = new File("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/AllQueryResults/2532/4.html");
		
		String htmlContent = ParsingStage.readFile(file);
		
		Parser parser = new Parser(new Lexer(new Page(htmlContent, "UTF-8")));
		
//		NodeIterator nodes = parser.elements();

		NodeList nl = parser.parse(new NodeFilter() {
			
			@Override
			public boolean accept(Node arg0) {
				return true;
			}
		});
		
		TagNode t = new TagNode();
		
		int index = 0;
		
		while (index < nl.size()){
			
			Node nn = (Node)nl.elementAt(index).clone();
						
			nn.setParent(t);
			
			System.out.println("PARENT: " + nn.getParent().getClass());
			
			print(nn);
		
			index++;
		}
		
	}

	private static void print(Node nextNode) {
			
		System.err.println(nextNode.toHtml());
		
		Node cl = NodeCloner.clone(nextNode, false, false);
		
		System.err.println(cl.toHtml());
				
		System.out.println(nextNode.hashCode() + " - " + nextNode.getClass());
		
		NodeList nl = nextNode.getChildren();
		
		int index = 0;
		
		while (nl!=null && index < nl.size()){
			
			print(nl.elementAt(index));
			
			index++;
			
		}
	}

}
